package com.hsdev.caprace;

import java.util.ArrayList;
import java.util.List;
import org.andengine.engine.camera.Camera;


import com.badlogic.gdx.math.Vector2;


public class GameManager {
	
	private static GameManager INSTANCE;
	private List<Kapsel> KapselList;
	public Vector2 kapselBialyStartPos = new Vector2(0, 0);
	public Vector2 kapselCzarnyStartPos = new Vector2(0, 0);
	
	
		
		
	
	
	
	
	private GameManager() {
		KapselList = new ArrayList<Kapsel>();
		
	}
	
	public static GameManager getInstance(){
		if(INSTANCE == null){
			INSTANCE = new GameManager();
		}
		return INSTANCE;
	}
	
	public void addKapselToList(Kapsel kapsel, int location){
		this.KapselList.add(location, kapsel);
		
	}
	
	public void nextMove(Kapsel kapsel, Camera camera){
		
		if(KapselList.indexOf(kapsel) == 0){
			KapselList.get(0).canMove = false;
			KapselList.get(1).canMove = true;
			//camera.setChaseEntity(KapselList.get(1));
		}else{
			
			KapselList.get(1).canMove = false;
			KapselList.get(0).canMove = true;
			//camera.setChaseEntity(KapselList.get(0));
		}
		
	}
	
	public void setCanMove(Kapsel kapsel, boolean canMove){
		kapsel.canMove = canMove;
	}
	
	public List getKapselList(){
		
		
		return KapselList;
		
	}
	
	public void rememberStartPositions(){
		
		kapselBialyStartPos.x = KapselList.get(0).body.getPosition().x;
		kapselBialyStartPos.y = KapselList.get(0).body.getPosition().y;
		
		kapselCzarnyStartPos.x = KapselList.get(1).body.getPosition().x;
		kapselCzarnyStartPos.y = KapselList.get(1).body.getPosition().y;
	}
	
	public void returnToStartPos(){
		KapselList.get(0).body.setLinearVelocity(0, 0);
		KapselList.get(1).body.setLinearVelocity(0, 0);
		KapselList.get(0).body.setTransform(kapselBialyStartPos, 0);
		KapselList.get(1).body.setTransform(kapselCzarnyStartPos, 0);
	}
	
	public boolean checkIfOutside(){
		boolean outside = false;
		
		if(KapselList.get(0).granice == 0 && KapselList.get(1).granice != 0){
			outside = true;
			
		}
		if(KapselList.get(0).granice != 0 && KapselList.get(1).granice == 0){
			outside = true;
		}
		if(KapselList.get(0).granice == 0 && KapselList.get(1).granice == 0){
			outside = true;
		}
		if(KapselList.get(0).granice != 0 && KapselList.get(1).granice != 0){
			//outside = false;
		}
		
		return outside;
		
	}
	
	public boolean checkIfStopped(){
		boolean stopped = false;
		if(Math.abs(KapselList.get(0).body.getLinearVelocity().x) < 0.1f && Math.abs(KapselList.get(0).body.getLinearVelocity().y) < 0.1f && Math.abs(KapselList.get(1).body.getLinearVelocity().x) < 0.1f && Math.abs(KapselList.get(1).body.getLinearVelocity().y) < 0.1f){
			stopped = true;
		}
		
		
		return stopped;
		
	}
	
	public void setChaseCamera(Camera camera){
		if(KapselList.get(0).canMove == true){
			camera.setChaseEntity(KapselList.get(0));
		}else{
			camera.setChaseEntity(KapselList.get(1));
		}
	}
	
}
